package com.cskaoyan.config2;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @author stone
 * @date 2022/09/05 14:47
 */
@Configuration
public class DatasourceConfiguration2 {

    // 写死了 → Magic Value 魔法值
    /*String driverClass = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/cskaoyan_db";
    String username = "root";
    String password = "123456";*/

    // 这些成员变量的值，我们期望从配置文件中获得
    @Value("${cskaoyan.datasource.driverClass}")
    String driverClass ; //com.mysql.jdbc.Driver";
    @Value("${cskaoyan.datasource.url}")
    String url ; //jdbc:mysql://localhost:3306/cskaoyan_db";
    @Value("${cskaoyan.datasource.username}")
    String username ; //root";
    @Value("${cskaoyan.datasource.password}")
    String password ; //123456";

    @Bean
    public DataSource dataSource2() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}
